package com.zikao.question.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zikao.article.entity.ArticleEntity;
import com.zikao.article.entity.CollectEntity;
import com.zikao.common.vo.TableListVo;
import com.zikao.question.entity.QuestionEntity;
import com.zikao.question.entity.Text2Entity;
import com.zikao.question.service.QuestionService;
import com.zikao.question.service.Text2Service;
import com.zikao.user.entity.UserEntity;
import com.zikao.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.zikao.question.entity.Collect2Entity;
import com.zikao.question.service.Collect2Service;
import com.zikao.common.utils.PageUtils;
import com.zikao.common.utils.R;



/**
 * 
 *
 * @author likaisheng
 * @email 1805318928@qq.com
 * @date 2021-03-03 20:46:00
 */
@Slf4j
@Controller
@RequestMapping("question/collect2")
public class Collect2Controller {
    @Autowired
    private Collect2Service collect2Service;
    @Autowired
    private UserService userService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private Text2Service text2Service;

    /**
     * 删除
     */
    @RequestMapping("/removeObj")
    @ResponseBody
    public R removeObj(@RequestBody Collect2Entity collect){
        Map<String,Object> collectMap = new HashMap<>();
        try{
            collectMap.put("question_id",collect.getQuestionId());
            collectMap.put("user_id",collect.getUserId());
            collect2Service.removeByMap(collectMap);
        }catch (Exception e){
            log.error("程序出现错误-removeObj",e);
            return R.error();
        }
        return R.ok();
    }

    @RequestMapping("/listForLayUI")
    @ResponseBody
    public TableListVo listForLayUI(@RequestParam Map<String, Object> params){
        QueryWrapper<Collect2Entity> queryWrapper = new QueryWrapper<>();
        // 是否传入了用户id
        String userIdStr = (String) params.get("userId");
        if (StringUtils.isNotBlank(userIdStr)){
            Long userId = Long.parseLong(userIdStr);
            queryWrapper.eq("user_id",userId);
        }

        PageUtils page = collect2Service.queryPage(params,queryWrapper);
        TableListVo<Collect2Entity> tableListVo = new TableListVo();

        tableListVo.setCode(0);
        tableListVo.setMsg("");
        tableListVo.setCount(page.getTotalCount());

        List<Collect2Entity> collectList = (List<Collect2Entity>) page.getList();
        List<Long> questionIds = collectList.stream().map((r) -> {
            return r.getQuestionId();
        }).collect(Collectors.toList());
        if (collectList.size()>0){
            List<QuestionEntity> questionList = questionService.selectBatchIds(questionIds);
            List<Long> userIds = questionList.stream().map(r -> {
                return r.getUserId();
            }).collect(Collectors.toList());
            List<UserEntity> userList = userService.selectBatchIds(userIds);
//            List<Long> textIds = questionList.stream().map(r -> {
//                return r.getTextId();
//            }).collect(Collectors.toList());
//            List<Text2Entity> textList = text2Service.selectBatchIds(textIds);
//            Map<Long, Text2Entity> textMap = textList.stream().collect(Collectors.toMap(v -> v.getId(), v -> v));
            Map<Long, QuestionEntity> questionMap = questionList.stream().collect(Collectors.toMap(v -> v.getId(), v -> v));
            Map<Long, UserEntity> userMap = userList.stream().collect(Collectors.toMap(v -> v.getId(), v -> v));

            for (Collect2Entity collect : collectList){
                QuestionEntity question = questionMap.get(collect.getQuestionId());
                UserEntity user = userMap.get(question.getUserId());
                collect.setQuestionTitle(question.getTitle());
                collect.setQuestionUserName(user.getName());
            }
        }

        tableListVo.setData(collectList);
        return tableListVo;
    }

    /**
     * 按参数获取点赞信息
     */
    @RequestMapping("/selectByMap")
    @ResponseBody
    public R selectByMap(@RequestBody Map<String, Object> params){
        List<Collect2Entity> collectList = collect2Service.selectByMap(params);
        return R.ok().put("collectList", collectList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = collect2Service.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Long id){
		Collect2Entity collect2 = collect2Service.getById(id);

        return R.ok().put("collect2", collect2);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody Collect2Entity collect2){
		collect2Service.save(collect2);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody Collect2Entity collect2){
		collect2Service.updateById(collect2);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody Long[] ids){
		collect2Service.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
